package com.sfx.concurrent.demo.concurrent.scheduleExecutor;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-03-29
 * Time: 23:13
 */

@Slf4j(topic = "c.TestError")
public class TestError {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        ScheduledExecutorService pool = Executors.newScheduledThreadPool(2);
//        pool.schedule(()->{
//            try {
//                log.debug("task...");
//                int x = 1/0;
//            } catch (Exception e) {
//                log.error("error : {}",e);
//            }
//        },1, TimeUnit.SECONDS);

        /**
         * 线程池处理异常
         * 1. 使用try catch 自己主动处理异常 使用日志进行记录
         * 2. 线程池提交任务的时候使用submit ==> 通过callable 配合future来实现
         * f.get()直接会把异常进行返回
         */

        ExecutorService pool = Executors.newFixedThreadPool(2);
        Future<Boolean> f = pool.submit(() -> {
            log.debug("task...");
            int x = 10/0;
            return true;
        });
        log.error("res : {}",f.get());
    }

}
